package com.syq.day1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author syq
 * 函数对象好处1：行为参数化
 */
public class Sample4 {

    static class Student {
        private String name;
        private String sex;
        private int age;

        public Student(String name, String sex, int age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }

        public String getName() {
            return name;
        }
        public int getAge() {
            return age;
        }
        public String getSex() {
            return sex;
        }

        @Override
        public String toString() {
            return "Student{name='" + name + "', sex='" + sex + "', age=" + age + "}";
        }
    }

    interface Lambda{
        boolean test(Student student);
    }

    //    static Lambda add2 = Integer::sum;    Integer::sum; == (a, b) -> a + b;
    static Lambda ageLambda = student -> student.getAge() > 20;
    static Lambda sexLambda = student -> student.getSex().equals("男");
    public static void main(String[] args) {
        List<Student> students =  Arrays.asList(
                new Student("张三", "男", 18),
                new Student("李四", "女", 19),
                new Student("王五", "男", 20),
                new Student("赵六", "女", 21),
                new Student("钱七", "男", 22)
        );

        // 筛选出男性学生
        System.out.println(filter(students, sexLambda));;

        // 筛选出年龄大于18岁的学生
        System.out.println(filter(students, ageLambda));

    }

    private static List<Student> filter(List<Student> students, Lambda lambda) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if(lambda.test(student)){
                result.add(student);
            }
        }
        return result;
    }
}
